

package com.wake.widget.widget.dialog;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;

import com.wake.widget.R;
import com.wake.widget.layout.NMUILinearLayout;
import com.wake.widget.skin.NMUISkinHelper;
import com.wake.widget.skin.NMUISkinValueBuilder;
import com.wake.widget.util.NMUIResHelper;

public class NMUITipDialogView extends NMUILinearLayout {

    private final int mMaxWidth;
    private final int mMiniWidth;
    private final int mMiniHeight;

    public NMUITipDialogView(Context context) {
        super(context);
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER_HORIZONTAL);
        int radius = NMUIResHelper.getAttrDimen(context, R.attr.nmui_tip_dialog_radius);
        Drawable background = NMUIResHelper.getAttrDrawable(context, R.attr.nmui_skin_support_tip_dialog_bg);
        int paddingHor = NMUIResHelper.getAttrDimen(context, R.attr.nmui_tip_dialog_padding_horizontal);
        int paddingVer = NMUIResHelper.getAttrDimen(context, R.attr.nmui_tip_dialog_padding_vertical);
        setBackground(background);
        setPadding(paddingHor, paddingVer, paddingHor, paddingVer);
        setRadius(radius);
        NMUISkinValueBuilder builder = NMUISkinValueBuilder.acquire();
        builder.background(R.attr.nmui_skin_support_tip_dialog_bg);
        NMUISkinHelper.setSkinValue(this, builder);
        builder.release();
        mMaxWidth = NMUIResHelper.getAttrDimen(context, R.attr.nmui_tip_dialog_max_width);
        mMiniWidth = NMUIResHelper.getAttrDimen(context, R.attr.nmui_tip_dialog_min_width);
        mMiniHeight = NMUIResHelper.getAttrDimen(context, R.attr.nmui_tip_dialog_min_height);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if(widthSize > mMaxWidth){
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, widthMode);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        boolean needRemeasure = false;
        if(getMeasuredWidth() < mMiniWidth){
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMiniWidth, MeasureSpec.EXACTLY);
            needRemeasure = true;
        }

        if(getMeasuredHeight() < mMiniHeight){
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMiniHeight, MeasureSpec.EXACTLY);
            needRemeasure = true;
        }

        if(needRemeasure){
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}
